package com.starsea.book.ui.newpage.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.starsea.book.R
import com.starsea.book.base.VMBaseFragment
import com.starsea.book.databinding.FragmentBookDepositoryBinding
import com.starsea.book.ui.adapter.MyFragmentStateAdapter
import com.starsea.book.ui.newpage.activity.BookDepositoryChildActivity
import com.starsea.book.ui.newpage.adapter.BookDepositoryAdapter
import com.starsea.book.ui.newpage.viewmodel.HomeViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-07-12 19 GMT+8
 * @desc :
 */
class BookDepositoryFragment :
    VMBaseFragment<HomeViewModel>(R.layout.fragment_book_depository) {
    lateinit var mAdapter: BookDepositoryAdapter
    private val binding by viewBinding(FragmentBookDepositoryBinding::bind)
    override val viewModel by viewModels<HomeViewModel>()
    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
        mAdapter = BookDepositoryAdapter(requireContext())
        binding.rvContent.layoutManager = GridLayoutManager(requireContext(), 2)
        binding.rvContent.adapter = mAdapter

        mAdapter.addOnItemChildClickListener(R.id.rrl_parent) { _, _, pos ->
            startActivity<BookDepositoryChildActivity>("typeId" to mAdapter.getItem(pos)!!.typeId!!)
        }

        val value = arguments?.getString("value")
        viewModel.getDepositoryCategory(value ?: "")

        viewModel.bookDepositoryCategoryResutl.observe(this) {
            when (it.status) {
                200 -> {
                    binding.tvDesc.text =
                        "共${it.data!!.totalNum}册，本周新增${it.data!!.weekAddNum}}册"
                    mAdapter.submitList(it.data!!.secTypeDtoOutList)
                }

                else -> {

                }
            }
        }
    }


    companion object {
        fun newInstance(value: String): BookDepositoryFragment {
            val args = Bundle()
            val fragment = BookDepositoryFragment()
            args.putString("value", value)
            fragment.arguments = args
            return fragment
        }
    }


}